
#pragma once

#include <cstdint>
#include <memory>
#include "connection.h"
#include "loop_event.hpp"
#include "message_logic.h"
#include "server.h"
#include "rtmp_message.h"

class RtmpConnection;
class RtmpServer;
class MediaSourceManager;

enum ConnectionStatus
{
    EmConnUnknow,
    EmConnected,
    EmConnHandshakeC0C1,
    EmConnHandshakeSuc,
};


struct ConnectionData
{
    const uint8_t * m_data;
    int       m_data_length;
    std::vector<uint8_t> m_send_msg;
    int       m_send_msg_length;
};

class RtmpMessageProcess
{
public:
private:

};

class RtmpServer : public std::enable_shared_from_this<RtmpServer>
{
public:
    RtmpServer(LoopEvent * loop);
    void StartServer(int port);
    std::shared_ptr<MediaSourceManager> GetMediaSourceMgr();
private:
    void OnConnection(Connection * connection);
    void ProcessMessage(Connection * connection, const uint8_t * data, int data_length); 
private:
    LoopEvent * m_loop_event;
    std::shared_ptr<Server> m_server;
    std::shared_ptr<MediaSourceManager> m_media_source_manager;
    std::shared_ptr<RtmpConnection> m_connection; 
};

class RtmpConnection : public std::enable_shared_from_this<RtmpConnection>
{
public:
    RtmpConnection(LoopEvent * loop, Connection * conn, RtmpServer * server);
    void ProcessMessage(Connection * connection, ConnectionData & connection_data);
private:
    void ProcessSendData(RtmpPacketMsg & response); 
private:
    RtmpServer * m_rtmp_server;
    ConnectionStatus m_status;
    LoopEvent  * m_loop_event;
    Connection * m_connection;
private:
    RtmpMessageProcessLogic m_logic;
};




